package com.litidea.dao.impl;

import static com.litidea.commons.dict.StaticDics.*;
import static com.litidea.commons.dict.StaticDics.CHANGE_REASON;
import static com.litidea.commons.dict.StaticDics.CHANGE_TICKET_STATUS;

import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import com.litidea.commons.utils.JSONUtils;
import com.litidea.dao.InternalChangeDAO;
import com.litidea.dao.abs.InternalChangeDAOAbs;
import com.litidea.dataobjects.ChangeTypeTestItemsDO;
import com.litidea.dataobjects.InternalChangeDO;

@SuppressWarnings("unchecked")
public class InternalChangeDAOImpl extends InternalChangeDAOAbs implements InternalChangeDAO {

	/**
	 * 修改变更状态
	 * @param params
	 */
	private void updateChangeStatus(Map<String, String> params){
		this.getSqlMapClientTemplate().update("change.updateChangeStatus", params);
	}
	/**
	 * 获取内部变更数量
	 * 
	 * @param params
	 * @return
	 */
	private Integer getInternalChangeCount(Map<String, String> params) {
		return (Integer) this.getSqlMapClientTemplate().queryForObject(
				"change.getInternalChangesCount", params);
	}

	@Override
	public JSONObject getInternalChangePageList(Map<String, String> params) {
		// TODO Auto-generated method stub
		List<InternalChangeDO> list = this.getSqlMapClientTemplate()
				.queryForList("change.getInternalChanges", params);

		for (InternalChangeDO internalChangeDO : list) {
			internalChangeDO.setEmergencyString(CHANGE_EMERGENCY_STATUS
					.get(String.valueOf(internalChangeDO.getEmergency())));
			internalChangeDO.setReasonString(CHANGE_REASON.get(String
					.valueOf(internalChangeDO.getReasonCode())));
			internalChangeDO.setStatusString(CHANGE_TICKET_STATUS.get(String
					.valueOf(internalChangeDO.getStatus())));
			if (internalChangeDO.getEffective() == 1)
				internalChangeDO.setEffectiveString("长期");
			else {
				internalChangeDO.setEffectiveString("临时<br/>起始日期: "
						+ internalChangeDO.getEffectiveStartDate() + " -- "
						+ internalChangeDO.getEffectiveEndDate());
			}
			internalChangeDO.setEvaImpactForReliabilityString(IMPACT_FOR_RELIABILITY.get(String.valueOf(internalChangeDO.getEvaImpactForReliability())));
			internalChangeDO.setEvaImpactForProductHandleString(IMPACT_FOR_PRODUCE_HANDLE.get(String.valueOf(internalChangeDO.getEvaImpactForProductHandle())));
			internalChangeDO.setMustVerifyString(OK_MAP.get(internalChangeDO.getMustVerify()));
			internalChangeDO.setMustAlertCustomerString(OK_MAP.get(internalChangeDO.getMustAlertCustomer()));
			internalChangeDO.setMustSpplierChangeString(OK_MAP.get(internalChangeDO.getMustSpplierChange()));
			internalChangeDO.setResultString(EVALUATION_RESULT.get(internalChangeDO.getResult()));
			internalChangeDO.setSampleIsQualifiedString(OK_MAP.get(internalChangeDO.getSampleIsQualified()));
			internalChangeDO.setSmallIsQualifiedString(OK_MAP.get(internalChangeDO.getSmallIsQualified()));
			internalChangeDO.setMiddleIsQualifiedString(OK_MAP.get(internalChangeDO.getMiddleIsQualified()));
			internalChangeDO.setIsFinishedAlertCustomerString(OK_MAP.get(internalChangeDO.getIsFinishedAlertCustomer()));
			internalChangeDO.setIsFinishedSupChangeString(OK_MAP.get(internalChangeDO.getIsFinishedSupChange()));
			internalChangeDO.setIsFinishedSecurityString(OK_MAP.get(internalChangeDO.getIsFinishedSecurity()));
			internalChangeDO.setChangeResultIsQualifyString(OK_MAP.get(internalChangeDO.getChangeResultIsQualify()));
			internalChangeDO.setIsApproveChangeString(OK_MAP.get(internalChangeDO.getIsApproveChange()));
			
			internalChangeDO.setResearchDeptIsBomChangedString(OK_MAP.get(internalChangeDO.getResearchDeptIsBomChanged()));
			internalChangeDO.setResearchDeptIsDesignDocChangedString(OK_MAP.get(internalChangeDO.getResearchDeptIsDesignDocChanged()));
			internalChangeDO.setResearchDeptIsDrawingChnagedString(OK_MAP.get(internalChangeDO.getResearchDeptIsDrawingChnaged()));
			
			internalChangeDO.setEngineeringDeptIsBomChangedString(OK_MAP.get(internalChangeDO.getEngineeringDeptIsBomChanged()));
			internalChangeDO.setEngineeringDeptIsMoldChangedString(OK_MAP.get(internalChangeDO.getEngineeringDeptIsMoldChanged()));
			internalChangeDO.setEngineeringDeptIsProcChangedString(OK_MAP.get(internalChangeDO.getEngineeringDeptIsProcChanged()));
			internalChangeDO.setEngineeringDeptIsQcChangedString(OK_MAP.get(internalChangeDO.getEngineeringDeptIsQcChanged()));
			internalChangeDO.setEngineeringDeptIsSopChangedString(OK_MAP.get(internalChangeDO.getEngineeringDeptIsSopChanged()));
			
			internalChangeDO.setQualityDeptIsInspectDocChangedString(OK_MAP.get(internalChangeDO.getQualityDeptIsInspectDocChanged()));
			internalChangeDO.setQualityDeptIsMarkInFirst3String(OK_MAP.get(internalChangeDO.getQualityDeptIsMarkInFirst3()));
			internalChangeDO.setQualityDpetIsTrainedString(OK_MAP.get(internalChangeDO.getQualityDpetIsTrained()));
			internalChangeDO.setQualityDpetIsUpgradeInspectionString(OK_MAP.get(internalChangeDO.getQualityDpetIsUpgradeInspection()));
			
			internalChangeDO.setProductDeptIsMaterialCheckedString(OK_MAP.get(internalChangeDO.getProductDeptIsMaterialChecked()));
			internalChangeDO.setProductDeptIsRegistedString(OK_MAP.get(internalChangeDO.getProductDeptIsRegisted()));
			internalChangeDO.setProductDeptIsTrainedString(OK_MAP.get(internalChangeDO.getProductDeptIsTrained()));
			
			internalChangeDO.setSummaryIsImpactShipMentString(OK_MAP.get(internalChangeDO.getSummaryIsImpactShipMent()));
			internalChangeDO.setSummaryIsNumberBeforeChangeProdString(OK_MAP.get(internalChangeDO.getSummaryIsNumberBeforeChangeProd()));
			internalChangeDO.setSummaryIsNumberBeforeChangeSupString(OK_MAP.get(internalChangeDO.getSummaryIsNumberBeforeChangeSup()));			
			
			// 组装实验项目字符串
			List<ChangeTypeTestItemsDO> testItemsList = this.getTestItemssByChangeId(internalChangeDO.getId());
			String temp = "<table class=\"table\" style=\"table-layout:fixed;word-break: break-all;word-wrap:break-word;\" width=\"100%\"><tbody><tr><td  width=\"50%\">实验项目名称</td><td>是否必须</td></tr>";
			for(ChangeTypeTestItemsDO changeTypeTestItemsDO : testItemsList){
				temp += "<tr><td>"+changeTypeTestItemsDO.getTestItemsName()+"</td><td>"+changeTypeTestItemsDO.getIsMustString()+"</td></tr>";
			}
			temp += "</tbody></table>";
			
			internalChangeDO.setTestItemsString(temp);
		}
		return JSONUtils.getPageJSON(list, this.getInternalChangeCount(params));
	}

	@Override
	public Integer addNewInternalChange(Map<String, String> params) {
		// TODO Auto-generated method stub
		Integer id = (Integer)this.getSqlMapClientTemplate().insert("change.addInternalChange",
				params);
		return id;
	}

	@Override
	public Integer createNewInternalChange(Map<String, String> params) {
		// TODO Auto-generated method stub
		Integer id = (Integer)this.getSqlMapClientTemplate().insert("change.addInternalChange",
				params);
		// 更新外部变更的内部变更号
		params.put("internalChangeId", String.valueOf(id));
		this.getSqlMapClientTemplate().update("externalchange.setInternalChangeId",params);
		return id;
	}
	@Override
	public String deleteInternalChange(Integer id) {
		// TODO Auto-generated method stub
		this.getSqlMapClientTemplate()
				.delete("change.deleteInternalChange", id);
		return "删除内部变更成功";
	}

	@Override
	public String editInternalChange(Map<String, String> params) {
		// TODO Auto-generated method stub
		this.getSqlMapClientTemplate().update("change.updateInternalChange",
				params);
		return "编辑内部变更成功";
	}

	@Override
	public String approveInternalChange(Map<String, String> params) {
		// TODO Auto-generated method stub
		this.getSqlMapClientTemplate().update("change.approveInternalChange",
				params);
		return "批准内部变更成功";
	}
	
	@Override
	public String rejectInternalChange(Map<String, String> params) {
		// TODO Auto-generated method stub
		this.getSqlMapClientTemplate().update("change.rejectInternalChange",
				params);
		return "拒绝内部变更成功";
	}

	@Override
	public String setVerifyTeam(Map<String, String> params) {
		// TODO Auto-generated method stub
		this.getSqlMapClientTemplate().update("change.setVerifyTeam",
				params);
		return "评审组长设置成功";
	}

	@Override
	public String setChangeType(Map<String, String> params) {
		// TODO Auto-generated method stub
		this.getSqlMapClientTemplate().update("change.setChangeType",
				params);
		// 判定的同时复制变更类别到litidea_change_test_plan
		this.getSqlMapClientTemplate().insert("change.copyChangeTestPlan",
				params);
		return "判定成功";
	}
	
	@Override
	public List<ChangeTypeTestItemsDO> getTestItemssByChangeId(Integer changeId) {
		// TODO Auto-generated method stub
		List<ChangeTypeTestItemsDO> list = this.getSqlMapClientTemplate().queryForList(
				"change.getTestItemsByChangeId", changeId);
		
		for(ChangeTypeTestItemsDO changeTypeTestItemsDO:list){
			changeTypeTestItemsDO.setIsMustString(OK_MAP.get(changeTypeTestItemsDO.getIsMust()));
		}
		
		return list;
	}
	
	@Override
	public void updateIsMust(Map<String, String> params) {
		// TODO Auto-generated method stub
		this.getSqlMapClientTemplate().update("change.updateIsMust", params);
	}
	
	@Override
	public void submitForTestPlanApproval(Map<String, String> params) {
		// TODO Auto-generated method stub
		this.getSqlMapClientTemplate().update("change.submitForTestPlanApproval", params);
	}
	
	@Override
	public void approveTestPlan(Map<String, String> params) {
		// TODO Auto-generated method stub
		this.getSqlMapClientTemplate().update("change.approveTestPlan", params);
	}
	
	@Override
	public void rejectTestPlan(Map<String, String> params) {
		// TODO Auto-generated method stub
		this.getSqlMapClientTemplate().update("change.rejectTestPlan", params);
	}

	@Override
	public String saveChangeEvaluation(Map<String, String> params) {
		// TODO Auto-generated method stub
		this.getSqlMapClientTemplate().update("change.saveChangeEvaluation", params);
		this.getSqlMapClientTemplate().insert("change.addChangeNotification", params);
		this.getSqlMapClientTemplate().update("change.setQualityDeptUser", params);
		return "保存变更评审成功";
	}

	@Override
	public String uploadResultReport(Map<String, String> params) {
		// TODO Auto-generated method stub
		// 样本
		String type = params.get("type");
		if("SAMPLE".equals(type)){
			this.getSqlMapClientTemplate().update("change.uploadSampleResultReport", params);
			params.put("rejectedReason", "样本阶段评审被拒绝-拒绝理由:" + params.get("comments"));
		}else if("SMALL".equals(type)){
			this.getSqlMapClientTemplate().update("change.uploadSmallResultReport", params);
			params.put("rejectedReason", "小批量阶段评审被拒绝-拒绝理由:" + params.get("comments"));
		}else if("MIDDLE".equals(type)){
			this.getSqlMapClientTemplate().update("change.uploadMiddleResultReport", params);
			params.put("rejectedReason", "中批量阶段评审被拒绝-拒绝理由:" + params.get("comments"));
		}
		
		// 如果是拒绝
		if("2".equals(params.get("isApproveChange"))){
			this.getSqlMapClientTemplate().update("change.rejectInTestPhase", params);
		}
		
		
		return "上传测试报告成功";
	}
	
	@Override
	public String finishUploadReport(Map<String, String> params){
		this.getSqlMapClientTemplate().update("change.updateChangeStatus", params);
		this.getSqlMapClientTemplate().update("change.setQualityDeptUser", params);
		return "操作成功";
	}
		
	@Override
	public String alertCustomerAndSupplier(Map<String, String> params) {
		// TODO Auto-generated method stub
		params.put("status", "9");
		this.getSqlMapClientTemplate().update("change.updateChangeStatus", params);
		this.getSqlMapClientTemplate().update("change.alertCustomerAndSupplier", params);
		return "操作成功";
	}
	@Override
	public String saveChangeNotification(Map<String, String> params) {
		// TODO Auto-generated method stub
		this.getSqlMapClientTemplate().update("change.updateChangeNotification", params);
		if(params.get("rejectReason")==null)
			params.put("status", "22");
		else
			params.put("status", "11");
		params.put("id", params.get("changeId"));
		this.getSqlMapClientTemplate().update("change.updateChangeStatus", params);
		return "确认变更评审成功";
	}
	
	@Override
	public String selectDeployDeptUsers(Map<String, String> params) {
		// TODO Auto-generated method stub
		this.getSqlMapClientTemplate().update("change.selectDeployDeptUsers", params);
		// 根据选择部门判断状态
		params.put("id", params.get("changeId"));
		
		this.updateChangeStatus(params);
		return "选择下发部门成功";
	}
	
	@Override
	public String deployChange(Map<String, String> params) {
		// TODO Auto-generated method stub
		
		Integer type = Integer.parseInt(params.get("type"));
		if(type == 17){
			this.getSqlMapClientTemplate().update("change.deployChangeToResearchDept", params);
		}else if(type == 18){
			this.getSqlMapClientTemplate().update("change.deployChangeToEngineeringDept", params);
		}else if(type == 19){
			this.getSqlMapClientTemplate().update("change.deployChangeToQualityDept", params);
		}else if(type == 20){
			this.getSqlMapClientTemplate().update("change.deployChangeToProductDept", params);
		}else if(type == 21){
			this.getSqlMapClientTemplate().update("change.deployChangeToSummary", params);
		}
		
		return "执行成功";
	}
	
	@Override
	public String finished(Map<String, String> params) {
		// TODO Auto-generated method stub
		params.put("status", "12");
		this.updateChangeStatus(params);
		// 如果有外部变更单号依赖，设置外部变更为完成
		this.getSqlMapClientTemplate().update("change.finishExternalChange", params);
		return "执行成功";
	}

}
